package com.estar.worktime.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.estar.workparent.entity.TmblWorktimeParentEntity;
import com.estar.worktime.entity.TmblWorktimeEntity;
import com.estar.worktime.entity.WorkPlaceAndDataVo;
import com.estar.worktime.service.TmblWorktimeServiceI;

@Component("excelWorkTimeTable")
@Scope("prototype")
public class ExcelWorkTimeTable {
	
	private static final Logger logger = Logger.getLogger(ExcelWorkTimeTable.class);

	@Autowired
	private TmblWorktimeServiceI tmblWorktimeService;
	@Autowired
	private SystemService systemService;
	
	private DateFormat d=new SimpleDateFormat("dd");
	
	public void excelTable(String title,String day, HttpServletRequest request, HttpServletResponse response,List<String> days,List<WorkPlaceAndDataVo> wdList,List<TmblWorktimeEntity> newlist){
		
		response.setContentType("application/vnd.ms-excel");
		OutputStream fOut = null;
		try{
				String browse = BrowserUtils.checkBrowse(request);
				if ("MSIE".equalsIgnoreCase(browse.substring(0, 4))) {
					response.setHeader("content-disposition",
							"attachment;filename=" + java.net.URLEncoder.encode(title, "UTF-8") + ".xls");
				} else {
					String newtitle = new String(title.getBytes("UTF-8"), "ISO8859-1");
					response.setHeader("content-disposition",
							"attachment;filename=" + newtitle + ".xls");//设置标题
				}
				HSSFWorkbook workbook = exportExcel(title,day,days,wdList,newlist);
				fOut = response.getOutputStream();
				workbook.write(fOut);
		
		}catch(Exception e){
			logger.error("打印排班表系统错误：",e);
		}finally{
			if(fOut!=null){
				try {
					fOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
		public HSSFWorkbook exportExcel(String title,String day,List<String> days,List<WorkPlaceAndDataVo> wdList,List<TmblWorktimeEntity> newlist){
		// 声明一个工作薄
			HSSFWorkbook wb = new HSSFWorkbook();
				try {
					
					// 生成一个表格
					Sheet sheet = wb.createSheet(title);
					// 设表名为中文名字,要ENCODING_UTF_16编码， 默认编码是用 ENCODING_COMPRESSED_UNICODE
					
					int rowIndex = 0;
					// 创建行
					// 创建字体
					Font font1 = createFont(wb,14, Font.BOLDWEIGHT_BOLD);
					Font font2 = createFont(wb,8, HSSFFont.BOLDWEIGHT_NORMAL,
							HSSFColor.GREY_80_PERCENT.index);
					Font font3 = createFont(wb,10, HSSFFont.BOLDWEIGHT_BOLD);
					Font font4 = createFont(wb,11, HSSFFont.BOLDWEIGHT_BOLD,
							HSSFColor.BLUE.index);
					// 创建式样
					CellStyle style2 = createStyle(wb,font2, HSSFCellStyle.BORDER_THIN,
							HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
							HSSFCellStyle.BORDER_THIN, HSSFCellStyle.ALIGN_CENTER,
							HSSFCellStyle.VERTICAL_CENTER, HSSFColor.BLUE.index,
							HSSFColor.RED.index, (short) 0);
					style2.setWrapText(true);

					CellStyle style4 = createStyleThinFrame(wb,font4,
							HSSFColor.GREY_25_PERCENT.index,
							HSSFColor.GREY_25_PERCENT.index);
					style4.setWrapText(true);
					
					Row row2 = createRow(sheet, rowIndex);
					row2.setHeight((short) 400);
					Cell cell = createCell(row2, 0, "      ");
					cell.setCellStyle(style4);
					
					sheet.setColumnWidth(0, 6000);
					int s=4000;
					for(int i=1;i<(days.size()+1);i++){
						Cell cell1 = createCell(row2, i, day+"-"+days.get(i-1).replace("日", ""));
						cell1.setCellStyle(style4);
						sheet.setColumnWidth(i, s);//设置列宽
					}
					int index = 1;
					for(WorkPlaceAndDataVo wp:wdList){
						rowIndex++;
						int celIndex =0;
						Row rowList = createRow(sheet, rowIndex);
						rowList.setHeight((short) 600);//设置行高
						Cell cellList0 = createCell(rowList, celIndex,wp.getWorkPlaceName());
						cellList0.setCellStyle(style4);
						int size=wp.getWtime().size()+1;
						for(int j=1;j<size;j++){
							TmblWorktimeEntity t=wp.getWtime().get(j-1);
							String mess="";
							for(TmblWorktimeEntity tw:newlist){
								if(t.getWorkPlaceId().equals(tw.getWorkPlaceId()) && t.getWorkdate().compareTo(tw.getWorkdate())==0){
									mess+=tw.getEmpname()+",";
								}
							}
							if(StringUtil.isEmpty(mess)){
								Cell cellList = createCell(rowList, celIndex+j,t.getEmpname());
								cellList.setCellStyle(style2);
							}else{
								if(StringUtil.isEmpty(t.getEmpname())){
									mess=mess.substring(0,mess.length()-1);
									Cell cellList = createCell(rowList, celIndex+j,mess);
									cellList.setCellStyle(style2);
								}else{
									Cell cellList = createCell(rowList, celIndex+j,mess+t.getEmpname());
									cellList.setCellStyle(style2);
								}
								
							}
						}
					}
					/*if(addList.size()>0 && addList!=null){//新增的排班信息
						for(int k=0;k<addList.size();k++){
							WorkPlaceAndDataVo wp=addList.get(k);
							rowIndex++;
							int celIndex =0;
							Row rowList = createRow(sheet, rowIndex);
							rowList.setHeight((short) 600);//设置行高
							Cell cellList0 = createCell(rowList, celIndex,wp.getWorkPlaceName());
							cellList0.setCellStyle(style4);
							
							TmblWorktimeEntity wt=new TmblWorktimeEntity();
							wt=wp.getWtime().get(0);
							for(TmblWorktimeEntity wt:wp.getWtime()){
								int size=days.size()+1;
								for(int j=1;j<size;j++){
									if(j==getNo(wt.getWorkdate())){
										
									}else{
										Cell cellList = createCell(rowList, celIndex+j,"");
										cellList.setCellStyle(style2);
									}
									
								}
							}
							
						}
					}*/
				} catch (Exception e) {
					e.printStackTrace();
				}
				return wb;
			}
		
		private Integer getNo(Date workDate){
			int i=1;
			String wdString=d.format(workDate);
			i=Integer.valueOf(wdString);
			return i;
			
		}
		
		
		/*public Font createFontBold() {
			return createFont(14, Font.BOLDWEIGHT_BOLD);
		}*/
		public Font createFont(HSSFWorkbook wb,int height, int boldWeight) {
			// fWorkbook = wb.createFont();
			Font font = wb.createFont();
			font.setFontHeightInPoints((short) height);
			font.setBoldweight((short) boldWeight);
			font.setFontName("Arial");

			return font;
		}
		public Font createFont(HSSFWorkbook wb,int height, int boldWeight, int color) {
			Font font = wb.createFont();
			font.setFontHeightInPoints((short) height);
			font.setBoldweight((short) boldWeight);
			font.setColor((short) color);
			font.setFontName("Arial");

			return font;
		}
		public CellStyle createStyle(HSSFWorkbook wb,Font font, int bottom, int top, int left,
				int right, int halign, int valign, int fore, int back, int pattern) {
			CellStyle style = wb.createCellStyle();
			style.setFont(font);
			style.setBorderBottom((short) bottom);
			style.setBorderTop((short) top);
			style.setBorderLeft((short) left);
			style.setBorderRight((short) right);
			style.setAlignment((short) halign);
			style.setVerticalAlignment((short) valign);
			style.setFillBackgroundColor((short) back);
			style.setFillForegroundColor((short) fore);
			style.setFillPattern((short) pattern);
			return style;
		}
		public CellStyle createStyleThinFrame(HSSFWorkbook wb,Font font3, int fore, int back) {
			CellStyle style = createStyle(wb,font3, HSSFCellStyle.BORDER_THIN,
					HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
					HSSFCellStyle.BORDER_THIN, HSSFCellStyle.ALIGN_CENTER,
					HSSFCellStyle.VERTICAL_CENTER, fore, back,
					HSSFCellStyle.SOLID_FOREGROUND);
			return style;
		}
		public Row createRow(Sheet sheet1, int rowIndex) {
			return sheet1.createRow(rowIndex);
		}
		public Cell createCell(Row row2, int cellIndex, String value) {
			Cell cell = row2.createCell((short) cellIndex);
			// 显示中文。
			// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(value);
			return cell;
		}
}
